package com.alan.service.impl;

import com.alan.entity.RestBean;
import com.alan.entity.Schedule;
import com.alan.entity.Word;
import com.alan.entity.Wordlist;
import com.alan.entity.vo.LearnVo;
import com.alan.mapper.ScheduleMapper;
import com.alan.mapper.VocabularyMapper;
import com.alan.service.ScheduleService;
import com.alan.service.VocabularyService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VocabularyServiceImpl implements VocabularyService {

    @Resource
    private VocabularyMapper vocabularyMapper;

    @Resource
    private ScheduleMapper scheduleMapper;

    @Override
    public RestBean getList() {

        List<Wordlist> wordlists = vocabularyMapper.getAllList();

        return RestBean.success(200,wordlists);
    }

    @Override
    public RestBean goLearn(Integer listId,Integer userId) {

        List<Word> words =  vocabularyMapper.getWordsByListId(listId);
        Schedule schedule = scheduleMapper.getScheduleByUserIdAndListId(listId, userId);

        LearnVo learnVo = new LearnVo(schedule,words);

        return RestBean.success(200,learnVo);
    }

    @Override
    public RestBean getReviseList(Integer userId) {

        List<Word> reviseList = vocabularyMapper.getReviseListByUserId(userId);

        return RestBean.success(reviseList);
    }

    @Override
    public RestBean setSchedule(Integer userId, Integer listId) {

        Wordlist wordlist = vocabularyMapper.getWordListById(listId);
        Integer wordNum = wordlist.getWordNum();
        int res = vocabularyMapper.insertSchedule(userId,listId,wordNum);
        if (res == 1)
            return RestBean.success(200);
        else {
            return RestBean.failure("系统错误，请联系管理员");
        }
    }

    @Override
    public RestBean updateReviseList(Integer userId, List<Integer> reviseList) {
        int count = 0;
        for(Integer wordId:reviseList){
            count += vocabularyMapper.insertReviseByUserId(userId,wordId);
        }
        return RestBean.success(200,count);
    }

    @Override
    public RestBean clearReviseList(Integer userId) {

        int count = vocabularyMapper.clearReviseListByUserId(userId);
        //TODO:所有的结果判断有待优化，可以使用统一异常处理（懒得搞了）
        if (count>0){
            return RestBean.success(count);
        }
        else {
            return RestBean.failure("系统错误，请联系管理员！");
        }
    }
}
